package controllers;

import helpers.CrawlerHelper;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;

import debugs.RecentlyDebug;

import entities.Recently;

import systems.RecentlyArticle;

public class SitemapServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response)
		throws IOException, ServletException {
		
		List<String> ids = RecentlyArticle.getArticles();
		
		List<String> urls = new ArrayList<String>();
		
		if (ids.size() > 10) {
			for (int i = 0; i < ids.size(); i++) {
				Recently recently = new Gson().fromJson(ids.get(i), Recently.class);
				urls.add(recently.getUrl());
				
			}
		} else {
			urls = CrawlerHelper.getUrlSitemap();
		}
		System.out.println(urls.size());
		request.setAttribute("urls", urls);
		try {
			request.getRequestDispatcher("/pages/sitemap.jsp").forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}
}
